//购物车操作模块
//数据操作模块
var sqlHelper = require('../conf/sqlHelper');

//向购物车添加商品
function insertCart(cartInfo,callback) {
	var sqlstr='insert into shop_cart(customer_Id,product_Id,num)values(?,?,?)';
    var param=[cartInfo.getcustomer_Id(),cartInfo.getproduct_Id(),cartInfo.getnum()];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }

    });
}

//如果购物车有商品，则更新数量
function updateCart(cartInfo,callback){
	var sqlstr='update shop_cart set num=num+? where customer_Id=? and product_Id=?';
    var param=[cartInfo.getnum(),cartInfo.getcustomer_Id(),cartInfo.getproduct_Id()];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }

    });
}

//判断当前购物车是否包含要添加的商品信息
function validateExistGoodsInCart(cartInfo,callback){
	var sqlstr = 'select count(*) as num from shop_cart where customer_Id=? and product_Id=?';
	var param = [cartInfo.getcustomer_Id(),cartInfo.getproduct_Id()];

	sqlHelper.ExecuteReader(sqlstr, param, function(result) {
		var count = result[0].num;
		if(count > 0) {
			callback(true);
		} else {
			callback(false);
		}
	});
}

//添加购物车综合方法
function addCart(cartInfo,callback){
    var message;
    //验证当前商品是否已经在购物车中
	validateExistGoodsInCart(cartInfo,function(count) {
		if(!count){
			//如果购物车没有当前购买商品则将商品添加到购物车
			insertCart(cartInfo,function (result) {
                message='购物车添加成功';
                //console.log('addCart:'+result);
				callback(message,result);
			});

		}else{
			//如果购物车内包含当前商品，则更新当前商品在购物车的数量
			updateCart(cartInfo,function(result){
                message='购物车更新成功';
                //console.log('addCart:'+result);
				callback(message,result);
			});
		}
	});
}

//加载购物车数据信息
function getAllCartData(customer_Id,callback) {
	var sqlstr='select * from shop_cart,shop_product where shop_cart.product_Id=shop_product.product_Id AND shop_cart.customer_Id=?';
    var param=[customer_Id];

    sqlHelper.ExecuteReader(sqlstr,param,function(results){
    	if(results){   
    		callback(results);
    	}    		
    });
}

//修改购物车产品数量
function updateCartPdNumByProductId(cartInfo,callback) {
	var sqlstr='update shop_cart set num=? where customer_Id=? and product_Id=?';
    var param=[cartInfo.getnum(),cartInfo.getcustomer_Id(),cartInfo.getproduct_Id()];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }

    });
}

//将指定商品从购物车移除
function removeGoodsFormCart(customer_Id,product_Id,callback) {
    var sqlstr='delete from shop_cart where customer_Id=? and product_Id=?';
    var param=[customer_Id,product_Id];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }
    });
}

//从购物车移除多条数据
function removeMultiGoodsFormCart(customer_Id,pdIDlist,callback) {
    var sqlstr="Call deleteMultiCart(?,?,',')";
    var param=[customer_Id,pdIDlist];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }
    });
}

//从购物车移动到收藏夹
function moveMultiGoodsFormCartToCollect(customer_Id,pdIDlist,callback){
    var sqlstr="Call moveGoodsFromCartToCollect(?,?,',')";
    var param=[customer_Id,pdIDlist];

    sqlHelper.ExecuteUpdate(sqlstr,param,function(counts){
        if(counts>0){
            callback(true);
        }else{
            callback(false);
        }
    });
}


exports.addCart=addCart;
exports.getAllCartData=getAllCartData;
exports.updateCartPdNumByProductId=updateCartPdNumByProductId;
exports.removeGoodsFormCart=removeGoodsFormCart;
exports.removeMultiGoodsFormCart=removeMultiGoodsFormCart;
exports.moveMultiGoodsFormCartToCollect=moveMultiGoodsFormCartToCollect;